package com.common.base.utils

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.text.TextUtils
import android.widget.Toast
import com.common.base.R
import com.common.base.utils.DeviceInfoUtil.getPkgName
import com.common.base.utils.DeviceInfoUtil.isIntentAvailable
import com.common.base.utils.DeviceInfoUtil.isPackageExist

/**
 *
 * 作者：sosou
 *
 * 版本：1.0
 *
 * 创建日期：10/2/2023
 *
 * 描述：
 *
 * 修订历史：
 *
 */
object AppMarketUtils {
    //小米应用商店
    const val PACKAGE_MI_MARKET = "com.xiaomi.market"
    const val MI_MARKET_PAGE = "com.xiaomi.market.ui.AppDetailActivity"

    //魅族应用商店
    const val PACKAGE_MEIZU_MARKET = "com.meizu.mstore"
    const val MEIZU_MARKET_PAGE = "com.meizu.flyme.appcenter.activitys.AppMainActivity"

    //VIVO应用商店
    const val PACKAGE_VIVO_MARKET = "com.bbk.appstore"
    const val VIVO_MARKET_PAGE = "com.bbk.appstore.ui.AppStoreTabActivity"

    //OPPO应用商店
    const val PACKAGE_OPPO_MARKET = "com.oppo.market"
    const val OPPO_MARKET_PAGE = "a.a.a.aoz"

    //华为应用商店
    const val PACKAGE_HUAWEI_MARKET = "com.huawei.appmarket"
    const val HUAWEI_MARKET_PAGE = "com.huawei.appmarket.service.externalapi.view.ThirdApiActivity"

    //ZTE应用商店
    const val PACKAGE_ZTE_MARKET = "zte.com.market"
    const val ZTE_MARKET_PAGE = "zte.com.market.view.zte.drain.ZtDrainTrafficActivity"

    //360手机助手
    const val PACKAGE_360_MARKET = "com.qihoo.appstore"
    const val PACKAGE_360_PAGE = "com.qihoo.appstore.distribute.SearchDistributionActivity"

    //酷市场 -- 酷安网
    const val PACKAGE_COOL_MARKET = "com.coolapk.market"
    const val COOL_MARKET_PAGE = "com.coolapk.market.activity.AppViewActivity"

    //应用宝
    const val PACKAGE_TENCENT_MARKET = "com.tencent.android.qqdownloader"
    const val TENCENT_MARKET_PAGE = "com.tencent.pangu.link.LinkProxyActivity"

    //PP助手
    const val PACKAGE_ALI_MARKET = "com.pp.assistant"
    const val ALI_MARKET_PAGE = "com.pp.assistant.activity.MainActivity"

    //豌豆荚
    const val PACKAGE_WANDOUJIA_MARKET = "com.wandoujia.phoenix2"

    // 低版本可能是 com.wandoujia.jupiter.activity.DetailActivity
    const val WANDOUJIA_MARKET_PAGE = "com.pp.assistant.activity.PPMainActivity"

    //UCWEB
    const val PACKAGE_UCWEB_MARKET = "com.UCMobile"
    const val UCWEB_MARKET_PAGE = "com.pp.assistant.activity.PPMainActivity"

    // 进入应用市场详情页
    fun gotoMarket(context: Context) {
        val intent = Intent(Intent.ACTION_VIEW)
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        //        intent.setData(Uri.parse("market://details?id=" + "com.jrd.beauty"));
        intent.data = Uri.parse("market://details?id=" + getPkgName(context))
        val keys = getKeys(context)
        if (keys != null) {
            intent.setClassName(keys[0]!!, keys[1]!!)
        }
        //修复某些老手机会因为找不到任何市场而报错
        if (isIntentAvailable(context, intent)) {
            context.startActivity(intent)
        } else {
            Toast.makeText(context, R.string.custom_no_market, Toast.LENGTH_SHORT).show()
        }
    }

    private fun getKeys(context: Context): Array<String?>? {
        val keys = arrayOfNulls<String>(2)
        if (isPackageExist(context, PACKAGE_MI_MARKET)) {
            keys[0] = PACKAGE_MI_MARKET
            keys[1] = MI_MARKET_PAGE
        } else if (isPackageExist(context, PACKAGE_VIVO_MARKET)) {
            keys[0] = PACKAGE_VIVO_MARKET
            keys[1] = VIVO_MARKET_PAGE
        } else if (isPackageExist(context, PACKAGE_OPPO_MARKET)) {
            keys[0] = PACKAGE_OPPO_MARKET
            keys[1] = OPPO_MARKET_PAGE
        } else if (isPackageExist(context, PACKAGE_HUAWEI_MARKET)) {
            keys[0] = PACKAGE_HUAWEI_MARKET
            keys[1] = HUAWEI_MARKET_PAGE
        } else if (isPackageExist(context, PACKAGE_ZTE_MARKET)) {
            keys[0] = PACKAGE_ZTE_MARKET
            keys[1] = ZTE_MARKET_PAGE
        } else if (isPackageExist(context, PACKAGE_COOL_MARKET)) {
            keys[0] = PACKAGE_COOL_MARKET
            keys[1] = COOL_MARKET_PAGE
        } else if (isPackageExist(context, PACKAGE_360_MARKET)) {
            keys[0] = PACKAGE_360_MARKET
            keys[1] = PACKAGE_360_PAGE
        } else if (isPackageExist(context, PACKAGE_MEIZU_MARKET)) {
            keys[0] = PACKAGE_MEIZU_MARKET
            keys[1] = MEIZU_MARKET_PAGE
        } else if (isPackageExist(context, PACKAGE_TENCENT_MARKET)) {
            keys[0] = PACKAGE_TENCENT_MARKET
            keys[1] = TENCENT_MARKET_PAGE
        } else if (isPackageExist(context, PACKAGE_ALI_MARKET)) {
            keys[0] = PACKAGE_ALI_MARKET
            keys[1] = ALI_MARKET_PAGE
        } else if (isPackageExist(context, PACKAGE_WANDOUJIA_MARKET)) {
            keys[0] = PACKAGE_WANDOUJIA_MARKET
            keys[1] = WANDOUJIA_MARKET_PAGE
        } else if (isPackageExist(context, PACKAGE_UCWEB_MARKET)) {
            keys[0] = PACKAGE_UCWEB_MARKET
            keys[1] = UCWEB_MARKET_PAGE
        }
        return if (TextUtils.isEmpty(keys[0])) {
            null
        } else {
            keys
        }
    }
}